<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="学院级别" prop="level">
        <el-radio-group v-model="dataForm.level">
          <el-radio v-for="(type, index) in dataForm.levelList" :label="index" :key="index">{{ type }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="学院名称" prop="name">
        <el-input v-model="dataForm.name" :placeholder="dataForm.levelList[dataForm.level] + '名称'"></el-input>
      </el-form-item>
      <el-form-item label="上级学院" prop="parentName">
        <el-input v-model="dataForm.parentName" v-popover:collegeListPopover :readonly="true" placeholder="点击选择上级学院" class="college-list__input"></el-input>
        <el-select-dropdown
          ref="collegeListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="collegeList"
            :props="collegeListTreeProps"
            node-key="collegeId"
            ref="collegeListTree"
            @current-change="collegeListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-select-dropdown>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="序号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          level: 1,
          levelList: ['一级学院', '二级学院', '专业'],
          name: '',
          parentId: 0,
          parentName: '',
          orderNum: 0
        },
        dataRule: {
          parentId: [
            { required: true, message: '上级学院不能为空', trigger: 'blur' }
          ],
          levelId: [
            { required: true, message: '学院级别不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '学院名称不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ]
        },
        collegeList: [],
        collegeListTreeProps: {
          label: 'name',
          children: 'children'
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/college/select'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          this.collegeList = treeDataTranslate(data.collegeList, 'collegeId')
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (!this.dataForm.id) {
            // 新增
            this.collegeListTreeSetCurrentNode()
          } else {
            // 修改
            this.$http({
              url: this.$http.adornUrl(`/sys/college/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              this.dataFormat.id = data.college.collegeId
              this.dataForm.level = data.college.level
              this.dataForm.name = data.college.name
              this.dataForm.parentId = data.college.parentId
              this.dataForm.orderNum = data.college.orderNum
              this.collegeListTreeSetCurrentNode()
            })
          }
        })
      },
      // 菜单树选中
      collegeListTreeCurrentChangeHandle (data, node) {
        this.dataForm.parentId = data.collegeId
        this.dataForm.parentName = data.name
      },
      // 菜单树设置当前选中节点
      collegeListTreeSetCurrentNode () {
        this.$refs.collegeListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.collegeListTree.getCurrentNode() || {})['name']
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/college/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'collegeId': this.dataForm.id || undefined,
                'level': this.dataForm.level,
                'name': this.dataForm.name,
                'parentId': this.dataForm.parentId,
                'orderNum': this.dataForm.orderNum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
